/**
 * Created by zjf on 2017/2/22.
 */
//shop scene
var ShopLayer = cc.Layer.extend({
    bg:null,
    ctor:function (args) {
        this._super();

        var HeroJson = JSON.parse(args);
        this.hero = new Hero(HeroJson);

        //show weapon，//for it's not good

        this.bg = new cc.Sprite(res.shopBg_jpg);
        var scaleX = size.width / this.bg.width;
        var scaleY = size.height / this.bg.height;
        this.bg.attr({
            anchorX: 0,
            anchorY: 0,
            scaleX: scaleX,
            scaleY: scaleY,
        });

        this.addChild(this.bg);

        for(var i=0,len=weaponArray.length;i<len;i++){
            if(this.hero.level >= weaponArray[i].needLevel && (this.hero.level-weaponArray[i].needLevel)<5){

                var BuyWeaponBtn = new ccui.Button(res.wuqi_gif);
                BuyWeaponBtn.attr({
                    weaponJson:weaponArray[i],///////////////
                    x:size.width/8+i*100,
                    y:size.height-size.height/3,
                    scaleX:2,
                    scaleY:2,
                });
                BuyWeaponBtn.addClickEventListener(this.BuyWeapon, this);
                this.addChild(BuyWeaponBtn);
            }
        }

        //return btn ,and return new heroText
        var ReturnBtn = new ccui.Button(res.return_png);
        ReturnBtn.attr({
            x:size.width-size.width/8 ,
            y:size.height-size.height/3,

        });
        ReturnBtn.addClickEventListener(this.returnGame, this);
        this.addChild(ReturnBtn);

        var attr = {
            x:500,
            y:100
        }

        gf.gradualShow(this, g.strShop,false,function(){
        },attr);

    },
    returnGame:function(){
        //cc.director.runScene(new GameScene());
        cc.director.popScene();
    },

    BuyWeapon:function(btn){
        var self = btn._parent;
        console.log(btn.weaponJson);
        if(g.gameRes.money >= btn.weaponJson.cost){
            self.hero.weapon = btn.weaponJson;
            g.gameRes.money -= btn.weaponJson.cost
        }
//        self.hero.weapon
    }



});

var ShopScene = cc.Scene.extend({
    ctor:function (args) {
        this._super();
        this.args = args;
    },
    onEnter:function () {
        this._super();
        var layer = new ShopLayer(this.args);
        this.addChild(layer);
    }
});